﻿module Sustainalytics.MostActiveReports {

    import Common = Sustainalytics.Common;

    export class MostActiveReportService implements IMostActiveReportService {

        private baseUrl: string = '/marserv/api/v1/MostActiveReport';

        constructor(
            private $q: ng.IQService,
            private $http: ng.IHttpService) {
        }

        public get(listOptions: Common.ListOptions): ng.IHttpPromise<any> {
            var ctx = this;

            var params = $.param(listOptions ? { listOptions: listOptions } : {});

            var promise = ctx.$http.get(ctx.baseUrl + '?' + params);
            return promise;
        }

        public addReport(companyId: string) {
            var ctx = this;

            ctx.$http.post(ctx.baseUrl + '/' + companyId, null);
        }

        public getList(skip: number, take: number): ng.IPromise<MostActiveReportDto[]> {
            var ctx = this;

            var def = ctx.$q.defer();

            var listOptions = new Common.ListOptions();
            listOptions.skip = skip;
            listOptions.take = take;

            var promise = ctx.get(listOptions);

            promise.then(
                // success
                function (res) {
                    def.resolve(res.data);
                },
                // err
                function (res) {
                    def.reject(res);
                });

            return def.promise;
        }
    }


    export var mostActiveReportServiceName = 'MostActiveReportService';

    angular.module(mostActiveReportsModuleName).service(mostActiveReportServiceName, MostActiveReportService);
} 